#include "monster.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

<<<<<<< HEAD
monster *CreateMonster(const char *name, int HP, int ATK,int DEF,char *symbol,int exp,int golden)
=======
monster *CreateMonster(const char *name, int HP, int ATK, char symbol,int exp,int golden)
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7
{
    monster *m = (monster*)malloc(sizeof(monster));
    if(m == NULL)
        return NULL;
    m->HP = HP;
    m->ATK = ATK;
<<<<<<< HEAD
    m->DEF = DEF;
=======
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7
    m->symbol = symbol;
    m->name = GetString(name);
    m->x = 0;
    m->y = 0;
    m->exp = exp;
    m->golden = golden;
    return m;
}

void FreeMonster(void *ptr)
{
    monster *m = (monster*)ptr;
    ClearString(&m->name);
    free(m);
}

void SetRandPoint(void *ptr)
{
    monster *m = (monster*)ptr;
    m->x = rand() % 10;
    m->y = rand() % 10;
    while(m->y == 0 && m->x == 0)
    {
        m->x = rand() % 10;
        m->y = rand() % 10;
    }
}


void SetMosters(DArray *ms)
{
    //设置随机种子
    srand(time(NULL));
    for(int i = 0; i < 10; i++)
    {
<<<<<<< HEAD
        ms->InsertArrayTail(ms,CreateMonster("史莱姆",10,1,2,"    😶",10,1));
=======
        ms->InsertArrayTail(ms,CreateMonster("史莱姆",10,1,'O',10,1));
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7
    }

    for(int i = 0; i < 8; i++)
    {
<<<<<<< HEAD
        ms->InsertArrayTail(ms,CreateMonster("哥布林",20,3,3,"    🧌 ",20,3));
=======
        ms->InsertArrayTail(ms,CreateMonster("哥布林",20,3,'G',20,3));
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7
    }

    for(int i = 0; i < 5; i++)
    {
<<<<<<< HEAD
        ms->InsertArrayTail(ms,CreateMonster("鬼剑士",30,5,5,"    👺",40,5));
    }

    ms->InsertArrayTail(ms,CreateMonster("黑  龙",50,10,10,"    🐲",0,0));
=======
        ms->InsertArrayTail(ms,CreateMonster("鬼剑士",30,5,'J',40,5));
    }

    ms->InsertArrayTail(ms,CreateMonster("黑龙🐉",50,10,'H',0,0));
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7

    ms->DisplayDynamicArray(ms,SetRandPoint);
    
}

<<<<<<< HEAD
static char *(*MyMap)[MAPSZIE] = NULL;
=======
static char (*MyMap)[MAPSZIE] = NULL;
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7
void SetPosition(void *ptr)
{
    monster *m = (monster*)ptr;
    MyMap[m->x][m->y] = m->symbol;
}

bool SortByPoint(void *ptr1,void *ptr2)
{
    monster *m1 = (monster*)ptr1;
    monster *m2 = (monster*)ptr2;
    if(m1->x > m2->x)
        return true;
    else if(m1->x == m2->x)
    {
        return m1->y > m2->y;
    }
    return false;
}

//位置去重
void SetPosDumplicate(DArray *ms)
{
    int flag = 0;
    ms->Sort(ms,SortByPoint);
    for(int i = 1; i < ms->len - 1; i++)
    {
        monster *m1 = (monster*)ms->dp[i];
        monster *m2 = (monster*)ms->dp[i+1];
        if(m1->x == m2->x && m1->y == m2->y)
        {
            m2->y++;
            if(m2->y == MAPSZIE)
            {
                flag = 1;
                m2->x++;
                m2->x = m2->x % MAPSZIE;
                m2->y = 1;
            }
        }
    }
    if(flag == 1)
    {
        SetPosDumplicate(ms);
    }
}

void PrintMonster(void *ptr)
{
    monster *m = (monster*)ptr;
    printf("name : %s : %d y : %d\n",m->name.string,m->x + 1,m->y + 1);
}

<<<<<<< HEAD
void SetMonsterPosition(DArray *ms, char *(*map)[MAPSZIE])
=======
void SetMonsterPosition(DArray *ms, char (*map)[MAPSZIE])
>>>>>>> c412670cb91f5a48c84219f2f6837419b34335e7
{
    //坐标去重
    SetPosDumplicate(ms);

    //在地图上显示怪兽
    MyMap = map;
    ms->DisplayDynamicArray(ms,SetPosition);
    MyMap = NULL;
    //ms->DisplayDynamicArray(ms,PrintMonster);
}

monster *GetMonsterByPosition(DArray *ms, int x, int y)
{
    for(int i = 0; i < ms->len; i++)
    {
        monster *m = (monster*)ms->dp[i];
        if(m->x == x && m->y == y && m->HP > 0)
        {
            return m;
        }
    }
    return NULL;
}
